package com.app.hmst.main;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.hmst.json.JSONParserForGetList;
import com.app.hmst.model.HomestayInfoVO;
import com.app.hmst.model.HomestayVO;
import com.app.hmst.model.IndicatorResultVO;
import com.app.hmst.utils.GlobalVariable;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.roscopeco.ormdroid.Entity;

@SuppressLint({ "NewApi", "ValidFragment" }) public class ViewAllEvaluatePageFragment extends Fragment{
	private View v;
	private ImageView menuIcon,sendEvaluate;
	private SlidingMenu _sm;
	private ListView evaluateListView;
	private HomestayInfoVO temp;
	private boolean invalid;
	private boolean isLogin;
	public ViewAllEvaluatePageFragment(SlidingMenu sm){
		this._sm = sm;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		System.gc();
		
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);
		
		
		v = inflater.inflate(R.layout.fragment_view_all_evaluate_page,container, false);
		sendEvaluate = (ImageView) v.findViewById(R.id.imageView1);
		menuIcon = (ImageView) v.findViewById(R.id.imageView6);
		evaluateListView = (ListView) v.findViewById(R.id.listView1);
		SharedPreferences sharedPref = getActivity().getSharedPreferences("MyPref",0);
		isLogin = sharedPref.getBoolean("isLogin", false);
		new InitAndLoadSubList().execute();
		menuIcon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				_sm.toggle();
			}
		});
		sendEvaluate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//switchFragment(new ViewAllSummaryEvaluatePageFragment(_sm));
				JSONParserForGetList.getInstance().postResult(1,getActivity());
			}
		});
		new Utils(getActivity()).onBackPressed(v, new HomePageFragment(_sm),null);
		return v;
	}
  
    public class AllEvaluateMenuAdapter extends BaseAdapter{
    	public List<HomestayInfoVO> evaluateList;
    	public ViewHolder viewHolder;
    	public AllEvaluateMenuAdapter(List<HomestayInfoVO> evaluateList){
    		this.evaluateList=evaluateList;
    	}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return evaluateList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return evaluateList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				viewHolder = new ViewHolder();
				LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.each_all_evaluate_list,null);
				viewHolder.homestayName = (TextView) convertView.findViewById(R.id.homestayName);
				viewHolder.homeAddress1 = (TextView) convertView.findViewById(R.id.homeAddress1);
				viewHolder.homeAddress2 = (TextView) convertView.findViewById(R.id.homeAddress2);
				viewHolder.editHomeStay = (ImageView) convertView.findViewById(R.id.imageView3);
				viewHolder.clickForViewProgress = (RelativeLayout) convertView.findViewById(R.id.each_second_menu_list);
				temp = evaluateList.get(position);
				viewHolder.homestayName.setText(temp.name);
				viewHolder.homeAddress1.setText(temp.district+" "+temp.province);
				viewHolder.homeAddress2.setText(temp.address);
			}else{
				convertView.setTag(viewHolder);
			}
			if(isLogin){
			viewHolder.editHomeStay.setOnClickListener(new OnEditListener(position,convertView,temp));
			viewHolder.clickForViewProgress.setOnClickListener(new OnViewListener(position,convertView,temp));
			}else{
				viewHolder.editHomeStay.setAlpha(0.45f)	;
				viewHolder.clickForViewProgress.setAlpha(0.45f)	;
			}
			return convertView;			
		}
    	
    }
    public class OnEditListener implements OnClickListener{
		private int _position;
		private View view;
		private HomestayInfoVO homestayInfoVO;
		public OnEditListener(int position,View btnView,HomestayInfoVO homestayInfoVO){
			this._position = position;
			this.view = btnView;
			this.homestayInfoVO =  homestayInfoVO;
		}
		
		@Override
		public void onClick(View v) {
			switchFragment(new EvaluateFormFragment(_sm, homestayInfoVO,true));
		}
		
	}
    public class OnViewListener implements OnClickListener{
		private int _position;
		private View view;
		private HomestayInfoVO homestayInfoVO;
		public OnViewListener(int position,View btnView,HomestayInfoVO homestayInfoVO){
			this._position = position;
			this.view = btnView;
			this.homestayInfoVO =  homestayInfoVO;
		}
		
		@Override
		public void onClick(View v) {
			switchFragment(new ViewAllSummaryEvaluatePageFragment(_sm,homestayInfoVO));
		}
		
	}
    public class ViewHolder{
    	public TextView homestayName,homeAddress1,homeAddress2;
    	public ImageView editHomeStay,sendAllResult;
    	public RelativeLayout clickForViewProgress;
    }
    public class InitAndLoadSubList extends AsyncTask<String, Void, List<HomestayInfoVO>> implements OnCancelListener{
		ProgressHUD mProgressHUD;
		public InitAndLoadSubList(){
			
		}
		
    	@Override
    	protected void onPreExecute() {
        	mProgressHUD = ProgressHUD.show(getActivity(),"Loading Content...", true,true,this);
    		super.onPreExecute();
    	}
		@Override
		protected List<HomestayInfoVO> doInBackground(String... params) {
			// TODO Auto-generated method stub
			
			List<HomestayInfoVO> result = Entity.query(HomestayInfoVO.class).where("status").ne("SC").executeMulti();
			for(HomestayInfoVO homestayInfoVO : result){
				List<IndicatorResultVO> indResultVOs = Entity
						.query(IndicatorResultVO.class)
						.where("homeId")
						.eq(homestayInfoVO.id)
						.and("houseId")
						.eq("0")
						.and("indicatorId")
						.in("select indicatorId From comapphmstmodelIndicatorResultVO ")
						.executeMulti();

				HomestayVO vo = new HomestayVO();
				HomestayVO homestayVO = Entity.query(HomestayVO.class).where("type").eq(GlobalVariable.TYPE_HOMESTAY).execute();
				if (indResultVOs == null
						|| indResultVOs.size() != vo.getFormSize(homestayVO)) {
					invalid=true;
					break;
				}
			}
			return result;
		}
		
		@Override
		protected void onPostExecute(List<HomestayInfoVO> result) {
			super.onPostExecute(result);
			if (result != null) {
				AllEvaluateMenuAdapter adapter = new AllEvaluateMenuAdapter(result);
				evaluateListView.setAdapter(adapter);			   
				mProgressHUD.dismiss();
			} else {
				mProgressHUD.dismiss();
			}
			if(invalid){
			sendEvaluate.setVisibility(View.GONE);
			}
		}
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub
			mProgressHUD.dismiss();
		}
	}
    public class TempEvaluate{
    	public String homestayName;
    	public String address;
    	public String address2;
		public String getHomestayName() {
			return homestayName;
		}
		public void setHomestayName(String homestayName) {
			this.homestayName = homestayName;
		}
		public String getAddress() {
			return address;
		}
		public void setAddress(String address) {
			this.address = address;
		}
		public String getAddress2() {
			return address2;
		}
		public void setAddress2(String address2) {
			this.address2 = address2;
		}
    	
    }
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof HomeStayMainActivity) {
			HomeStayMainActivity fca = (HomeStayMainActivity) getActivity();
			fca.switchContent(fragment);
		}
	}

}
